package com.example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author loubobooo
 * @Description #131. 分割回文串
 * @Date 2022/3/4
 */
public class PalindromePartitioning {

    private List<List<String>> lists = new ArrayList<>();

    public List<List<String>> partition(String s) {
        if (s == null || s.length() == 0) {
            return lists;
        }
        backtracking(0, s, new ArrayList<>());
        return lists;
    }

    private void backtracking(int start, String s, List<String> list) {
        if (start == s.length()) {
            lists.add(new ArrayList<>(list));
            return;
        }
        for (int i = start; i < s.length(); i++) {
            String s1 = s.substring(start, i + 1);
            StringBuilder stringBuilder = new StringBuilder(s1);
            if (stringBuilder.reverse().toString().equals(s1)) {
                list.add(s1);
                // i+1表示不能重复读取
                backtracking(i + 1, s, list);
                list.remove(list.size() - 1);
            }
        }
    }
}
